package cn.kgc.dao;

import cn.kgc.pojo.NewsDetail;
import cn.kgc.util.DatabaseUtil;

import javax.xml.soap.Detail;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class NewsDetailDaoImpl extends BaseDao implements  NewsDetailDao {


    @Override
    public List<NewsDetail> getNewsDestailList(Connection connection) {
        //1.加载数据库驱动

        ResultSet rs=null;
        List<NewsDetail> list =new ArrayList<>();
        try {
            //3.创建statement对象执行sql 返回ResultSet
            String sql ="select id ,categoryId,title,summary,content,picPath,author,createDate from news_detail ";
            rs=execuseQuery(connection,sql);
            //4.遍历ResultSet 获取数据，封装数据
            while(rs.next()){
                //JAVA类的属性=数据库的字段
                int id =rs.getInt(1);
                int categoryid =rs.getInt(2);
                String summary= rs.getString(3);
                String content=  rs.getString(4);
                String title= rs.getString(5);
                String picpath= rs.getString(6);
                String author =rs.getString(7);
                Date date =rs.getDate(8);
                NewsDetail newsDetail =new NewsDetail(id,categoryid,summary,content,title,picpath,author,date,null);
                list.add(newsDetail);
            }
        } catch ( SQLException e) {
            e.printStackTrace();
        }
        //5.关闭资源
        DatabaseUtil.closeResult(null,pmstm,rs);

        //6.返回数据局
        return  list;

    }

    @Override
    public int insertNewsdetail(Connection connection,NewsDetail newsDetail) {

        //1.加载数据库驱动
        PreparedStatement pstmst=null;
        List<NewsDetail> list =new ArrayList<>();
        int rs=0;

            //3.创建statement对象执行sql 返回ResultSet ？是占位符，表示一个数据，将来通过setObject赋值，可以防止注入攻击，安全姓高
            //所以不要用statement了，这个是容易出现拼接sql、
            // String sql="select count from user where userName="+name+"and password="+password  但是有人密码穿的是  xxx or  where 1 =1
            String sql ="insert into news_detail(categoryId,title,summary,content,picPath,author,createDate) value(?,?,?,?,?,?,now())";

            rs= execuseUpdate(connection,
                    sql,
                    newsDetail.getCategoryId(),
                    newsDetail.getTitle(),
                    newsDetail.getSummary(),
                    newsDetail.getContent(),
                    newsDetail.getPicPath(),newsDetail.getAuthor());

        DatabaseUtil.closeResult(null,pstmst,null);
        return rs;
    }


    public int getNewsListCount(Connection connection, String title, Integer categoryId) throws SQLException {
        //1.写sql语句title=? and categoryId=?
       StringBuffer str=new StringBuffer(" select count(id) from news_detail where 1=1");
       List list =new ArrayList();
       if(title!=null&&!title.equals("")){
            str.append(" and title like concat('%',?,'%')");
            list.add(title);
       }

       if(categoryId!=null){
           str.append(" and categoryId=?");
           list.add(categoryId);
       }


        System.out.println(str.toString());
        //2.调用BaseDao的查询方法，把参数放进去可以了
        ResultSet rs= execuseQuery2(connection,str.toString(),list.toArray());

        //3.返回resultSet
        int i=0;
        while(rs.next()){
           i=rs.getInt(1);
        }

        //4.取数据返回
        return i;
    }

    @Override
    public List<NewsDetail> getNewsDetailList(Connection connection, String title, Integer categoryId,Integer index,Integer pageSize) throws SQLException {
        StringBuilder sql = new StringBuilder("select * from news_detail where 1=1");
        List listPms = new ArrayList();
        List<NewsDetail> rList = new ArrayList();
        if (title != null && !title.equals("")){
            sql.append(" and title like ?");
            listPms.add("%"+title+"%");
        }
        if (categoryId != null){
            sql.append(" and categoryId=?");
            listPms.add(categoryId);
        }

        sql.append(" limit ?,?");
        listPms.add(index);
        listPms.add(pageSize);

        ResultSet rs = execuseQuery2(connection, sql.toString(), listPms.toArray());
        while(rs.next()){
            //JAVA类的属性=数据库的字段
            int id =rs.getInt(1);
            int categoryid =rs.getInt(2);
            String summary= rs.getString(3);
            String content=  rs.getString(4);
            String rtitle= rs.getString(5);
            String picpath= rs.getString(6);
            String author =rs.getString(7);
            Date date =rs.getDate(8);
            NewsDetail newsDetail =new NewsDetail(id,categoryid,summary,content,rtitle,picpath,author,date,null);
            rList.add(newsDetail);
        }
        return rList;
    }

    @Override
    public NewsDetail getNewsDetailById(Connection connection, Integer id) {
        String sql ="select id ,categoryId,title,summary,content,picPath,author,createDate from news_detail where id =?";
        ResultSet rs=execuseQuery(connection,sql,id);
        //4.遍历ResultSet 获取数据，封装数据
        try{
        while(rs.next()){
            //JAVA类的属性=数据库的字段
            int sid =rs.getInt(1);
            int categoryid =rs.getInt(2);
            String summary= rs.getString(3);
            String content=  rs.getString(4);
            String title= rs.getString(5);
            String picpath= rs.getString(6);
            String author =rs.getString(7);
            Date date =rs.getDate(8);
            NewsDetail newsDetail =new NewsDetail(sid,categoryid,summary,content,title,picpath,author,date,null);
            return newsDetail;
        }
    } catch ( SQLException e) {
        e.printStackTrace();
    }
        return null;
    }

    @Override
    public int deleteNewsDetail(Connection connection, Integer id) {
        String sql= "delete from news_detail where id =? ";
        int rs= execuseUpdate(connection,sql,id);
        DatabaseUtil.closeResult(connection,null,null);
        return rs;
    }

    @Override
    public int updateNewsDetail(Connection connection, NewsDetail newsDetail) {
        String sql="update news_detail set categoryId=? ," +
                "title=? ,summary=?,content=?, picPath=? ,author=? where id=?";
       int rs=execuseUpdate(connection,
                sql,
                newsDetail.getCategoryId(),
                newsDetail.getTitle(),
                newsDetail.getSummary(),
                newsDetail.getContent(),
                newsDetail.getPicPath(),
                newsDetail.getAuthor(),
               newsDetail.getId());
        DatabaseUtil.closeResult(connection,null,null);
        return rs;
    }


}
